// pages/courseOnline/courseOnline.js
import {
  reqCurseList
} from '../../api/courseOnline'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    option1: [{
        text: '价格',
        value: 0
      },
      {
        text: '升序',
        value: 1
      },
      {
        text: '降序',
        value: 2
      },
    ],
    pageNo: 1,
    channelId: 409,
    pageSize: 5,
    listData: [],
    loading: 0,
    value1: 0,
  },

  // 返回
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  // 跳转到详情
  toDetail(event) {
    // console.log(event.currentTarget);
    let id = event.currentTarget.dataset.id;
    // console.log(id);
    wx.navigateTo({
      url: '/pages/courseTeachRoom/courseTeachRoom?id=' + id,
    })
  },
  onLoad(options) {
    this.getData();
  },
  // 触底事件
  onReachBottom() {
    let page = this.data.pageNo + 1;
    if (this.data.loading == 2) {
      return;
    }
    this.setData({
      pageNo: page,
      loading: 1
    })
    this.getData();
  },
  // 获取数据
  async getData() {
    let {
      pageNo,
      pageSize,
      channelId
    } = this.data;
    let res = await reqCurseList({
      channelId,
      pageNo,
      pageSize
    });
    // console.log(res);
    if (res.code == 1) {
      if (res.data.length == 0) {
        this.setData({
          loading: 2
        });
      } else {
        this.setData({
          loading: 0
        });
      }
      this.setData({
        listData: [...this.data.listData, ...res.data.list]
      });
      // console.log(this.data.listData);
    }
  },
})